package cn.zero.letaomallspringboot.controller;

import cn.zero.letaomallspringboot.Constant.ResultCode;
import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.common.Result;
import cn.zero.letaomallspringboot.entity.GoodCategory;
import cn.zero.letaomallspringboot.service.IGoodCategoryService;
import cn.zero.letaomallspringboot.service.impl.GoodCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goodCategory")
public class GoodCategoryController {

    private final IGoodCategoryService goodCategoryService;

    private static final Logger logger = LoggerFactory.getLogger(GoodCategoryController.class);

    public GoodCategoryController(IGoodCategoryService goodCategoryService) {
        this.goodCategoryService = goodCategoryService;
    }

    /**
     * 分页查询分类
     */
    @GetMapping("/list")
    public Result<PageResult<GoodCategory>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        logger.info("分页查询分类：{} to {}", page, size);
        PageResult<GoodCategory> result = goodCategoryService.listByPage(page, size);
        return Result.success(result);
    }

    /**
     * 查询所有分类（不分页）
     */
    @GetMapping("/listAll")
    public Result<List<GoodCategory>> listAll() {
        logger.info("查询所有分类");
        List<GoodCategory> list = goodCategoryService.listAll();
        return Result.success(list);
    }

    /**
     * 根据ID查询分类
     */
    @GetMapping("/{id}")
    public Result<GoodCategory> getById(@PathVariable Integer id) {
        logger.info("根据id查询分类：{}", id);
        GoodCategory category = goodCategoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable Integer id) {
        logger.info("根据id删除分类：{}", id);
        boolean success = goodCategoryService.deleteById(id);
        return success ? Result.success(true) : Result.fail(ResultCode.CATEGORY_DELETE_FAIL);
    }

    /**
     * 新增分类
     */
    @PostMapping
    public Result<Boolean> insert(@RequestBody GoodCategory category) {
        logger.info("新增分类：{}", category);
        boolean success = goodCategoryService.insert(category);
        return success ? Result.success(true) : Result.fail(ResultCode.CATEGORY_INSERT_FAIL);
    }

    /**
     * 更新分类
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody GoodCategory category) {
        logger.info("更新分类：{}", category);
        boolean success = goodCategoryService.update(category);
        return success ? Result.success(true) : Result.fail(ResultCode.CATEGORY_UPDATE_FAIL);
    }

    /**
     * 更新状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(
            @PathVariable Integer id,
            @RequestParam Integer status) {
        logger.info("更新分类状态：{},{}", id, status);
        boolean success = goodCategoryService.updateStatus(id, status);
        return success ? Result.success(true) : Result.fail(ResultCode.CATEGORY_STATUS_UPDATE_FAIL);
    }
}